package com.hildert.shop.controllers;

import com.hildert.shop.entities.Good;
import com.hildert.shop.entities.Pagination;
import com.hildert.shop.services.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GoodController {

    @Autowired
    private GoodService goodService;

    @RequestMapping(value = "/goods", method = RequestMethod.GET)
    public Pagination<Good> getList(@PageableDefault(value = 10, sort = { "created" }, direction = Sort.Direction.DESC)
                                      Pageable pageable) {
        Page<Good> data = goodService.getList(pageable);

        Pagination<Good> pagination = new Pagination<>();
        pagination.setPage(pageable.getPageNumber());
        pagination.setSize(data.getSize());
        pagination.setTotal(data.getTotalElements());
        pagination.setData(data.getContent());

        return pagination;
    }
}
